package com.yb.guigu.pick.mail.sender.sender;

import com.yb.guigu.pick.mail.sender.bean.MailSenderRequest;
import com.yb.guigu.pick.mail.sender.bean.MailSenderResponse;
import com.yb.guigu.pick.core.constant.EmailType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/11 20:11
 * FileName: IGuiguMainSender
 * Description:
 */

public class GuiguMailSenderImpl implements IGuiguMailSender{

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    MailProperties mailProperties;

    @Override
    public MailSenderResponse send(MailSenderRequest mailSenderRequest) {
        String type = mailSenderRequest.getType();
        MailSenderResponse.MailSenderResponseBuilder builder = MailSenderResponse.builder().uuid(mailSenderRequest.getUuid());

        if(!StringUtils.hasText(type)){
            return builder.success(false).error("type不能为空").build();
        }

        if(type.equalsIgnoreCase(EmailType.SIMPLE.getName())){
            return sendSimple(mailSenderRequest);
        }else if(type.equalsIgnoreCase(EmailType.MIME.getName())){
            return sendMime(mailSenderRequest);
        }


        return builder.success(false).type("不支持的右键类型："+type).build();
    }

    private MailSenderResponse sendSimple(MailSenderRequest mailSenderRequest){
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject(mailSenderRequest.getSubject());
            mailMessage.setText(mailSenderRequest.getContent());
//发给别人
            mailMessage.setTo(mailSenderRequest.getSendTo());
//发给自己
            mailMessage.setFrom(mailProperties.getUsername());

            javaMailSender.send(mailMessage);
            return MailSenderResponse.builder().success(true).type(mailSenderRequest.getType())
                    .sendTo(mailSenderRequest.getSendTo())
                    .subject(mailSenderRequest.getSubject())
                    .uuid(mailSenderRequest.getUuid()).build();
        }catch (Exception e){
            return MailSenderResponse.builder().success(false).error(e.getMessage()).build();
        }

    }


    private MailSenderResponse sendMime(MailSenderRequest mailSenderRequest){
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper minehelper = new MimeMessageHelper(message, true);
            minehelper.setFrom(mailProperties.getUsername()); //设置发送邮件账号
            minehelper.setTo(mailSenderRequest.getSendTo()); //设置接收邮件的人，可以多个
            minehelper.setSubject(mailSenderRequest.getSubject()); //设置发送邮件的主题
            minehelper.setText(mailSenderRequest.getContent(), true); //设置发送邮件的内容 第二个设置为true则可以发送带HTML的邮件
            javaMailSender.send(message);
            return MailSenderResponse.builder().success(true).type(mailSenderRequest.getType())
                    .sendTo(mailSenderRequest.getSendTo())
                    .subject(mailSenderRequest.getSubject())
                    .uuid(mailSenderRequest.getUuid()).build();
        } catch (MessagingException e) {
            return MailSenderResponse.builder().success(false).error(e.getMessage()).build();
        }
    }
}
